<?php

namespace App\Http\Controllers\Api;

use App\Attachment;
use App\Collect;
use App\Http\Requests\StorePost;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class PostController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $orderBy = $request->order_by ? $request->order_by : $this->orderBy;
        $orderType = $request->order_type ? $request->order_type : $this->orderType;
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = Post::orderBy($orderBy, $orderType);
        if ($request->title) {
            $lists->where('title', 'like', '%' . $request->title . '%');
        }
        if ($request->category_id) {
            $lists->where('category_id', $request->category_id);
        }
        return $lists->with('category')->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * @param StorePost $request
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function store(StorePost $request)
    {
        $post = Post::create(array_merge($request->except('attachments'), ['user_id' => Auth::id()]));
        if ($request->attachments) {
            foreach ($request->attachments as $attachment) {
                Attachment::where('id', $attachment['id'])->update(['attachmentable_id' => $post->id]);
            }
        }
        return response($post);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Post $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        $info = $post;
        $post->increment('pv');
        if ($info) {
            $is_follow = 0;
            if (Collect::where('user_id', Auth::id())->where('news_id', $info->id)->first()) {
                $is_follow = 1;
            }
            $info->is_follow = $is_follow;
        }
        return $info;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Post $post
     * @return \Illuminate\Http\Response
     */
    public function edit(Post $post)
    {
        $post->attachments;
        return response($post);
    }

    /**
     * @param StorePost $request
     * @param Post $post
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function update(StorePost $request, Post $post)
    {
        $rs = $post->update($request->only(['title', 'thumb', 'body', 'keywords', 'category_id', 'description', 'from', 'editor', 'order', 'pv', 'is_published', 'editor_type']));
        if ($rs) {
            $attachments = $post->attachments;
            //清理附件
            foreach ($attachments as $attachment) {
                $attachment->attachmentable_id = 0;
                $attachment->save();
            }
            //关联附件
            if ($request->attachments) {
                foreach ($request->attachments as $attachment) {
                    Attachment::where('id', $attachment['id'])->update([
                        'attachmentable_id' => $post->id
                    ]);
                }
            }
            return response(['message' => '修改成功']);
        }
        return response(['message' => '修改失败'], 400);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Post $post
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
        $attachments = $post->attachments;
        if ($attachments) {
            foreach ($attachments as $attachment) {
                if ($attachment->url && Storage::disk('public')->exists($attachment->url))
                    Storage::disk('public')->delete($attachment->url);
                $attachment->delete();
            }
        }
        $post->delete();
        return response(['message' => '删除成功']);
    }

    public function latestPostOfEachCategory()
    {
        $tnswh = Post::where('category_id', 2)->latest()->first();
        $tnsdsh = Post::where('category_id', 3)->latest()->first();
        return response([
            $tnswh,
            $tnsdsh,
        ]);
    }
}
